package com.mystudy.leetcode.problem.link.p_206;

import com.mystudy.leetcode.base.ListNode;
import com.mystudy.leetcode.base.ListNodeUtils;
import org.junit.Test;

/**
 * @author 周志龙
 * @Email 15216876513@163.com
 * @Date on 2019/7/5 0:01
 * @Description 反转链表
 */
public class Solution {

    @Test
    public void testReverse() {
        ListNode listNode = ListNodeUtils.generateLinkList(new int[]{1, 2, 3, 4, 5});
        ListNode listNode1 = reverseList(listNode);
        ListNodeUtils.show(listNode1);
    }

    public ListNode reverseList(ListNode head) {
        //迭代
        ListNode prev = head;
        ListNode cur = head;
        while(cur != null&& cur.next!=null){
            ListNode nextTemp = cur.next;
            cur.next = nextTemp.next;
            nextTemp.next = prev;
            prev= nextTemp;
        }
        return prev;
    }
}
